﻿using System;
using System.Drawing;
using System.Windows.Forms;
using DC2010.Objects;
using DC2010;
using System.IO;

namespace DC2010_Editor.Controls.Utils
{
    public partial class CreatureGfxForm : Form
    {
        private CreatureGfx _igfx { get; set; }
        public CreatureGfxForm(CreatureGfx igfx)
        {
            _igfx = igfx;
            InitializeComponent();

            SetControls();
        }

        private void SetControls()
        {
            if (_igfx != null)
            {

                TextBoxFace.Text = _igfx.Face;
                TextBoxAttack.Text = _igfx.Attack.stex;
                TextBoxAway.Text = _igfx.Away.stex;
                TextBoxRight.Text = _igfx.Right.stex;
                TextBoxLeft.Text = _igfx.Left.stex;
                TextBoxFront.Text = _igfx.Front.stex;
            }
        }

        private void GetControls()
        {
            if (_igfx != null)
            {
                _igfx.Face = TextBoxFace.Text;
                _igfx.Attack.stex = TextBoxAttack.Text;
                _igfx.Away.stex = TextBoxAway.Text;
                _igfx.Right.stex = TextBoxRight.Text;
                _igfx.Left.stex = TextBoxLeft.Text;
                _igfx.Front.stex = TextBoxFront.Text;
            }
        }

        private void crystalButton3_Click(object sender, EventArgs e)
        {
            GetControls();
            Root.I.CreatureSystem.SaveInfo();
            Close();
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            GetControls();
            Root.I.CreatureSystem.SaveInfo();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            GetControls();
            FacePB.Refresh();

            AttackPB.Refresh();
            RightPB.Refresh();
            LeftPB.Refresh();
            AwayPB.Refresh();
            pictureBox1.Refresh();
        }

        private Image GetImage(string partialPath)
        {
            string s = Environment.CurrentDirectory + "\\Gfx\\" + partialPath+".png";
            if (File.Exists(s))
            {
                return Image.FromFile(s);
            }

            return null;
        }

        private void FloorPB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Attack.stex != null)
            {
                Image img = GetImage(_igfx.Attack.stex);
                if (img != null)
                {
                        e.Graphics.DrawImage(img, 0, 0);

                }
            }
        }

        private void SidePB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Away.stex != null)
            {
                Image img = GetImage(_igfx.Away.stex);
                if (img != null)
                {
                        e.Graphics.DrawImage(img, 0, 0);

                }
            }

        }

        private void TowardsPB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Right.stex != null)
            {
                Image img = GetImage(_igfx.Right.stex);
                if (img != null)
                {
                        e.Graphics.DrawImage(img, 0, 0);

                }
            }

        }

        private void AwayPB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Left.stex != null)
            {
                Image img = GetImage(_igfx.Left.stex);
                if (img != null)
                {
                        e.Graphics.DrawImage(img, 0, 0);

                }
            }

        }

        private void FacePB_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Face != null)
            {
                Image img = GetImage(_igfx.Face);
                if (img !=null)
                    e.Graphics.DrawImage(img, 0, 0);
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (_igfx.Front.stex != null)
            {
                Image img = GetImage(_igfx.Front.stex);
                if (img != null)
                {
                        e.Graphics.DrawImage(img, 0, 0);
                }
            }
        }
    
    }
}
